package com.wx.mall.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.common.exception.BusinessException;
import com.wx.mall.common.exception.code.AreaResponseCode;
import com.wx.mall.sys.entity.Area;
import com.wx.mall.sys.mapper.AreaMapper;
import com.wx.mall.sys.service.IAreaService;
import com.wx.mall.sys.vo.req.AreaReqVO;
import com.wx.mall.sys.vo.resp.AreaRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  区域设置服务实现类
 * </p>
 *
 * @author ljl
 * @since 2020-03-19
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {

    @Override
    public List<AreaRespVO> getAreasByParentId(Integer parentId) {
        // 查询所有分类
        List<Area> areas = baseMapper.selectList(null);
        // 类型转换
        List<AreaRespVO> datum = JSON.parseArray(JSON.toJSONString(areas),AreaRespVO.class);
        // 结果集
        List<AreaRespVO> result = getTree(datum,parentId);
        return result;
    }

    @Override
    public List<AreaRespVO> getAreasByTier(Integer tier) {
        // 查询所有分类
        List<Area> areas = baseMapper.selectList(new QueryWrapper<Area>().le("tier",tier));
        // 类型转换
        List<AreaRespVO> datum = JSON.parseArray(JSON.toJSONString(areas),AreaRespVO.class);
        // 结果集
        List<AreaRespVO> result = getTree(datum,0);
        return result;
    }

    @Override
    public List<AreaRespVO> getAreas() {
        List<AreaRespVO> areas = getAreasByParentId(0);
        return areas;
    }

    @Override
    public void add(AreaReqVO vo) {
        Area area = new Area();
        //将vo中的数据复制到实体类中
        BeanUtils.copyProperties(vo, area);
        //插入数据到数据库
        int result = baseMapper.insert(area);
        //新增失败抛出异常
        if (result == 0) {
            throw new BusinessException(AreaResponseCode.INSERT_ERROR);
        }
    }

    /**
     * 获取区域树状列表
     * @param list
     * @param parentId
     * @return
     */
    private List<AreaRespVO> getTree(List<AreaRespVO> list,Integer parentId){
        List<AreaRespVO> result = new ArrayList<>();
        //非空且长度大于0
        if (list != null && list.size() > 0){
            //循环遍历列表数据
            for (AreaRespVO temp : list){
                if (temp.getParentId() == parentId){
                    //获取父级id下所有子级区域
                    result.add(temp);
                    setChildren(list,temp);
                }
            }
        }
        return result;
    }

    /**
     * 递归设置区域树
     * @param list
     * @param areaVO
     */
    private void setChildren(List<AreaRespVO> list,AreaRespVO areaVO){
        List<AreaRespVO> result = new ArrayList<>();
        //循环遍历列表数据
        for (AreaRespVO temp : list){
            //是否有下级
            if (temp.getParentId() == areaVO.getId()){
                //获取父级id下所有子级区域
                result.add(temp);
                //递归
                setChildren(list,temp);
            }
        }
        areaVO.setChildren(result);
    }
}
